/**
 * 创建者：duyh
 * 方法描述：根据绑定的tab的ID值转向指定的页面
 * 所作用的页面名称：main.html
 * 变更时间：
 * 变更人：
 * 变更内容：
 **/
var tuichu = "再按一次退出程序";
var height = document.documentElement.clientHeight;
var searchTitle = document.getElementById("searchTitle");
var meTitle = document.getElementById("meTitle");
var projectTitle = document.getElementById("projectTitle");
var policyTitle = document.getElementById("policyTitle");
var resourcesTitle = document.getElementById("resourcesTitle");
var projectTab = document.getElementById("projectTab");
(function(e) {
	//预加载首页
	mui.init({
		keyEventBind: {
			backbutton: true //开启back按键监听
		}
	});
	clientWidth = document.documentElement.clientWidth;
	setPxPerRem(clientWidth); // 初始化方法
	mui.plusReady(function() {
		plus.navigator.setStatusBarBackground('#329ECF');
		var self = plus.webview.currentWebview();
		var isLogin = localStorage.getItem("isLogin");

		if(!isLogin) {
			hiddenEle(projectTab);
		} else {
			showEle(projectTab);
		}

		for(var i = 0; i < subpages.length; i++) {
			var temp = {};
			var sub;
			sub = plus.webview.create(subpages[i], subpages[i], subpage_style, subpage_value);
			if(i > 0) {
				sub.hide();
			} else {
				temp[subpages[i]] = "true";
				mui.extend(aniShow, temp);
			}
			self.append(sub);
		}

		var backButtonPress = 0;

		mui.back = function(event) {
			backButtonPress++;
			if(backButtonPress > 1) {
				plus.runtime.quit();
			} else {
				plus.nativeUI.toast(tuichu);
			}
			setTimeout(function() {
				backButtonPress = 0;
			}, 1000);
			return false;
		};
	});
})(mui);

var subpages = ['page_index.html', 'page_policy.html', 'page_resources.html', 'page_project.html', 'page_me.html'];
var subpage_style = {
	top: '50px',
	bottom: '51px'
};

var subpage_value = {
	clientWidth: clientWidth,
	pageflag: 'page_main.html',
	height: height - 101
}
var aniShow = {};

//当前激活选项
var activeTab = subpages[0];
//获取当前标题栏
//var title = document.getElementById("title");
var header = document.getElementById("header");
var xz_num = 0;
//选项卡点击事件
mui('.mui-bar-tab').on('tap', 'a', function(e) {
	var targetTab = this.getAttribute('href');
	if(targetTab == activeTab) {
		return;
	}
	var isLogin = localStorage.getItem("isLogin");

	if(targetTab == "page_project.html") {
		if(!isLogin) {
			deleteClass('mui-active', this);
			mui.toast("请登录后查看");
			return;
		}
	}

	for(var i = 0; i < subpages.length; i++) {
		if(targetTab == subpages[i] && i + 1 == xz_num) {
			return;
		}
	}
	var tabID = "";
	allDefault();
	//更换标题
	if(targetTab == subpages[1]) {
		xz_num = 2;
		tabID = "policyTab";
		document.getElementById("policyImg").src = "../../ui/building/images/02main/massage-active.png";
		document.getElementById("positionIcon").src = "../../ui/building/images/02main/02.png"
		showEle(document.getElementById("positionIcon"));
		showTitle(policyTitle)
	} else if(targetTab == subpages[2]) {
		xz_num = 3;
		tabID = "resourcesTab";
		document.getElementById("resourcesImg").src = "../../ui/building/images/02main/type-active.png";
		document.getElementById("positionIcon").src = "../../ui/building/images/02main/02.png"
		showTitle(resourcesTitle);
	} else if(targetTab == subpages[3]) {
		isLogin = localStorage.getItem("isLogin");
		xz_num = 4;
		tabID = "projectTab";
		showTitle(projectTitle);
		document.getElementById("positionIcon").src = "../../ui/building/images/02main/02.png"
		document.getElementById("projectImg").src = "../../ui/building/images/02main/project-active.png";
	} else if(targetTab == subpages[4]) {
		xz_num = 5;
		tabID = "settingTab";
		showTitle(meTitle);
		hiddenEle(document.getElementById("positionIcon"));
		document.getElementById("settingImg").src = "../../ui/building/images/02main/me-active.png";
		var targetView = plus.webview.currentWebview().children()[4];
		var token = localStorage.getItem("token_type") + " " + localStorage.getItem("access_token");
		targetView.evalJS("getFavoriteNum('" + token + "')");
	} else {
		xz_num = 1;
		tabID = "mainTab";
		showTitle(searchTitle);
		document.getElementById("positionIcon").src = "../../ui/building/images/02main/03.png";
		showEle(document.getElementById("positionIcon"));
		document.getElementById("mainImg").src = "../../ui/building/images/02main/main-active.png";
	}
	//显示目标选项卡
	//若为iOS平台或非首次显示，则直接显示
	if(mui.os.ios || aniShow[targetTab]) {
		plus.webview.show(targetTab);
	} else {
		//否则，使用fade-in动画，且保存变量
		var temp = {};
		temp[targetTab] = "true";
		mui.extend(aniShow, temp);
		plus.webview.show(targetTab, "fade-in", 300);
	}

	plus.webview.show(targetTab);
	//	}
	//隐藏当前;
	plus.webview.hide(activeTab);
	//更改当前活跃的选项卡
	activeTab = targetTab;
});
//自定义事件，模拟点击“首页选项卡”
document.addEventListener('gohome', function() {
	clickTab("mainTab")
}, false);

function clickTab(tabName) {
	var defaultTab = document.getElementById(tabName);
	//模拟首页点击
	mui.trigger(defaultTab, 'tap');
	//切换选项卡高亮
	var current = document.querySelector(".mui-bar-tab>.mui-tab-item.mui-active");
	if(defaultTab !== current) {
		current.classList.remove('mui-active');
		defaultTab.classList.add('mui-active');
	}
}

function allDefault() {
	document.getElementById("mainImg").src = "../../ui/building/images/02main/main.png";
	document.getElementById("policyImg").src = "../../ui/building/images/02main/massage.png";
	document.getElementById("resourcesImg").src = "../../ui/building/images/02main/type.png";
	document.getElementById("projectImg").src = "../../ui/building/images/02main/project.png";
	document.getElementById("settingImg").src = "../../ui/building/images/02main/me.png";
}

var positionDiv = document.getElementById("positionDiv");
function showTitle(ele) {
	hiddenEle(searchTitle);
	hiddenEle(meTitle);
	hiddenEle(projectTitle);
	hiddenEle(policyTitle);
	hiddenEle(resourcesTitle);
//	if(ele.id == "resourcesTitle") {
//		showEle(positionDiv);
//	} else {
//		hiddenEle(positionDiv);
//	}
	showEle(ele);
}

document.getElementById("searchTitle").addEventListener('click', function() {
	var postData = {
		pageflag: "page_main.html",
		activeTab: activeTab
	}

	var change1 = {
		pageUrl: "page_search.html",
		postData: postData,
		aniShow: "fade-in"
	}
	pageChange(change1);
}, false);

document.getElementById("positionIcon").addEventListener('click', function() {
	if(this.src.indexOf("02main/03.png") > -1) {
		var postData = {
			pageflag: "main.html"
		}

		var pageChange1 = {
			pageUrl: "page_searchAddress.html",
			postData: postData,
			aniShow: "fade-in"
		}
		pageChange(pageChange1);
	} else {
		var postData = {
			pageflag: "page_main.html",
			activeTab: activeTab
		}

		var change1 = {
			pageUrl: "page_search.html",
			postData: postData,
			aniShow: "fade-in"
		}
		pageChange(change1);
	}

}, false);

window.addEventListener('changeTab', function(event) {
	var tabId = event.detail.tabId;
	var itemid = event.detail.itemId;
	clickTab(tabId)
	if(tabId == "resourcesTab") {
		var targetView = plus.webview.currentWebview().children()[2];
		targetView.evalJS("clearnAndLoad('" + itemid + "' , ',,,')");
	}
}, false);

window.addEventListener('exitLogin', function() {
	var ws = plus.webview.currentWebview();
	plus.webview.close(ws);
	plus.navigator.setStatusBarBackground('#FFFFFF');
});